package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.enums.AuditStatusEnum;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;

import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAuditor;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAuditor;
import com.jzo2o.customer.mapper.WorkerCertificationAuditorMapper;

import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;

import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tyj
 * @since 2025-02-10
 */
@Service
public class WorkerCertificationAuditorServiceImpl extends ServiceImpl<WorkerCertificationAuditorMapper, WorkerCertificationAuditor> implements IWorkerCertificationAuditorService {


    @Autowired
    WorkerCertificationMapper workerCertificationMapper;

    @Autowired
    IWorkerCertificationService workerCertificationService;

    @Override
    public PageResult<WorkerCertificationAuditResDTO> selectPage(WorkerCertificationAuditPageQueryReqDTO reqDTO) {
        PageResult<WorkerCertificationAuditResDTO> result = PageHelperUtils.selectPage(reqDTO,
                () -> baseMapper.queryList(reqDTO));
        return result;
    }

    @Override
    public void audit(Long id, String rejectReason, Integer certificationStatus) {
        WorkerCertification workerCertification = workerCertificationMapper.selectById(id);
        WorkerCertificationAuditor workerCertificationAuditor = baseMapper.selectById(id);
        if (ObjectUtil.isEmpty(workerCertification) || ObjectUtil.isEmpty(workerCertificationAuditor)){
            throw new ForbiddenOperationException("数据异常 无法审批");
        }

        if (certificationStatus.equals(CertificationStatusEnum.SUCCESS.getStatus())) {
            //通过
            //1.判断状态是否可以修改
            if (workerCertification.getCertificationStatus().equals(CertificationStatusEnum.PROGRESSING.getStatus())
                    || workerCertification.getCertificationStatus().equals(CertificationStatusEnum.FAIL.getStatus())
                    && workerCertificationAuditor.getAuditStatus().equals(AuditStatusEnum.UNREVIEWED.getStatus())) {
                workerCertification.setCertificationStatus(CertificationStatusEnum.SUCCESS.getStatus());
                int flag1 = workerCertificationMapper.updateById(workerCertification);
                workerCertificationAuditor.setAuditStatus(AuditStatusEnum.CHECKED.getStatus());
                workerCertificationAuditor.setAuditorName(UserContext.currentUser().getName());
                workerCertificationAuditor.setAuditTime(LocalDateTime.now());
                workerCertificationAuditor.setAuditorId(UserContext.currentUserId());
                int flag2 = baseMapper.updateById(workerCertificationAuditor);

                if (flag1 != 1 && flag2 != 1) {
                    throw new CommonException("通过失败");
                }
            }else{
                throw new ForbiddenOperationException("状态异常 无法审批");
            }
        } else if (certificationStatus.equals(CertificationStatusEnum.FAIL.getStatus())) {
            //驳回
            //1.判断状态是否可以修改
            if (workerCertification.getCertificationStatus().equals(CertificationStatusEnum.PROGRESSING.getStatus())
                    && workerCertificationAuditor.getAuditStatus().equals(AuditStatusEnum.UNREVIEWED.getStatus())) {
                workerCertification.setCertificationStatus(CertificationStatusEnum.FAIL.getStatus());
                int flag1 = workerCertificationMapper.updateById(workerCertification);
                workerCertificationAuditor.setAuditStatus(AuditStatusEnum.CHECKED.getStatus());
                workerCertificationAuditor.setAuditorName(UserContext.currentUser().getName());
                workerCertificationAuditor.setAuditTime(LocalDateTime.now());
                workerCertificationAuditor.setAuditorId(UserContext.currentUserId());
                workerCertificationAuditor.setRejectReason(rejectReason);
                int flag2 = baseMapper.updateById(workerCertificationAuditor);

                if (flag1 != 1 && flag2 != 1) {
                    throw new CommonException("驳回失败");
                }

            }else {
                throw new ForbiddenOperationException("状态异常 无法审批");
            }
        }
    }

    @Override
    public WorkerCertificationAuditor rejectReason() {
        //查询认证失败的数据
        WorkerCertification workerCertification = workerCertificationService
                .lambdaQuery()
                .eq(WorkerCertification::getCertificationStatus,CertificationStatusEnum.FAIL.getStatus())
                .eq(WorkerCertification::getId,UserContext.currentUserId())
                .one();

        if (ObjectUtil.isEmpty(workerCertification)){
            throw new ForbiddenOperationException("数据异常 未查到驳回数据");
        }


        return lambdaQuery()
                .eq(WorkerCertificationAuditor::getId,UserContext.currentUserId())
                .eq(WorkerCertificationAuditor::getAuditStatus,AuditStatusEnum.CHECKED.getStatus())
                .one();
    }
}
